import { Staff } from "@/global/global";
import { api } from "@/global/utils/api";
import { userStateKey } from "@/store/userStore";
import { useStore } from "vuex";

export default function useStaff() {
  const userStore = useStore(userStateKey);

  const fetchRemoteLoggedStaffToLocal = () => new Promise((resolve, reject) => {
    api.get('/staff/me').then(res => {
      if (res.data.code == 200) {
        userStore.commit('setStaff', res.data.data);
        resolve(res.data.data);
      } else {
        console.log(`Wrong status code with data ${res.data}`)
        throw 'Status Code Is Not 200';
      }
    }).catch(e => {
      reject(e);
    })
  })

  const getLocalStaff = () => {
    return userStore.state.staff;
  }

  const getStaffByPage = (page, countPerPage, onlyDepartment: boolean = false) => {
    if (!onlyDepartment)
      return api.get(`/staff/getStaff/${page}/${countPerPage}`);
    else 
      return api.get(`/staff/getStaff/${page}/${countPerPage}/${getLocalStaff().department.id}`);
  }

  return { fetchRemoteLoggedStaffToLocal, getLocalStaff, getStaffByPage };
}